package com.xblibo.xruth.controller;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import net.mlw.vlh.ValueList;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.xblibo.xruth.common.BaseController;
import com.xblibo.xruth.entity.Attendance;
import com.xblibo.xruth.entity.User;
import com.xblibo.xruth.service.AttendanceService;
import com.xblibo.xruth.service.UserService;


/**
 * @Title: AttendanceController.java
 * @Package com.xblibo.xruth.controller
 * @Description TODO(添加描述)
 * @author xblibo
 * @date 2011-07-15 17:49:08
 * @version V1.0
 */

@Controller
public class AttendanceController extends BaseController{

	@Resource(name="attendanceService")
	private AttendanceService attendanceService;
	
	@Resource(name="userService")
	private UserService userService;

	@RequestMapping(value = "/attendance/list")
	public String list(HttpServletRequest request){
		params.put("companyId",getCurrentUser().getCompanyId());
		ValueList  valueList  =  getValueList("attendance_list", params, request);
		request.setAttribute("valueList", valueList);
		return "/attendance/attendance_list";
	}

	@RequestMapping(value = "/attendance/add",method=RequestMethod.GET)
	public String add(HttpServletRequest request){
		List<User> users = userService.findBy("companyId", getCurrentUser().getCompanyId());
		System.out.println(users.size());
		request.setAttribute("users",users);
		return "/attendance/attendance_add";
	}
	@RequestMapping(value = "/attendance/add",method=RequestMethod.POST)
	public String save(HttpServletRequest request){
		Attendance attendance = new Attendance();
		attendance.setUser(getCurrentUser());
		attendance.setStartIp(request.getRemoteAddr());
		attendance.setCompanyId(getCurrentUser().getCompanyId());
		attendance.setCompanyName(getCurrentUser().getCompanyName());
		attendance.setStartDate(new Date());
		attendanceService.saveOrUpdate(attendance);
		return "/attendance/attendance_add";
	}

	@RequestMapping(value = "/attendance/modify",method = RequestMethod.GET)
	public String modify(Integer attendanceId,HttpServletRequest request){
		Attendance attendance = attendanceService.findById(attendanceId);
		attendance.setEndDate(new Date());
		attendance.setEndIp(request.getRemoteAddr());
		attendanceService.saveOrUpdate(attendance);
		return "/attendance/attendance_modify";
	}
	
	@RequestMapping(value = "/attendance/modify",method = RequestMethod.POST)
	public String update(Integer attendanceId){
		Attendance attendance = attendanceService.findById(attendanceId);
		attendance.setEndDate(new Date());
		attendanceService.saveOrUpdate(attendance);
		return "/attendance/attendance_modify";
	}

	@RequestMapping(value = "/attendance/detail")
	public String detail(){
		
		return "/attendance/attendance_detail";
	}

}
